package com.icehand.environment;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * 环境变量读取与绑定
 * Created by icehand on 2017/5/12.
 */
@Configuration
public class MyEnvironmentAware implements EnvironmentAware {

	@Value("${spring.datasource.url}")
	private String datasourceUrl;

	@Override
	public void setEnvironment(Environment environment) {
		//注入的属性信息
		System.out.println("datasourceUrl = "+datasourceUrl);

		//根据environment获取系统属性
		System.out.println(environment.getProperty("JAVA_HOME"));

		//通过 environment 同样能获取到application.properties配置的属性
		System.out.println(environment.getProperty("spring.datasource.url"));

		//获取到前缀是"spring.datasource." 的属性列表值.
		RelaxedPropertyResolver relaxedPropertyResolver = new RelaxedPropertyResolver(environment,"spring.datasource.");
		System.out.println("spring.datasource.url="+relaxedPropertyResolver.getProperty("url"));

		System.out.println("spring.datasource.driverClassName="+relaxedPropertyResolver.getProperty("driverClassName"));
	}
}
